package com.hcrazy.manchester.app.controllers;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.NewsSort;
import com.hcrazy.manchester.app.services.NewsSortService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 新闻分类管理
 * @author Hsmart
 *
 */
@Controller
@RequestMapping("/admin/news")
public class NewsSortController {
	
	@Autowired
	private NewsSortService newsSortService;
	
	@RequestMapping(value="/reportSort",method=RequestMethod.GET)
	public void reportSort(){
		
	}
	
	@RequestMapping(value="/newsSort",method=RequestMethod.GET)
	public void newsSort(){
		
	}
	
	@RequestMapping(value="/addReportSort",method=RequestMethod.GET)
	public void addReportSort(@RequestParam int parentId,Model model){
		addChildSort(parentId,model);
		
	}
	
	@RequestMapping(value="/addNewsSort",method=RequestMethod.GET)
	public void addNewsSort(@RequestParam int parentId,Model model){
		addChildSort(parentId,model);
		
	}
	
	/**
	 * 跳转到添加子分类页面
	 * 初始化数据
	 * @param parentId
	 * @param model
	 */
	public void addChildSort(int parentId,Model model){
		//根据父分类id获取父分类对象
		try {
			NewsSort parentSort = new NewsSort();
			if(parentId == 0){
				parentSort.setSortId(0);
				parentSort.setSortName("无");
			}else{
				parentSort = newsSortService.getSortById(parentId);
			}
			model.addAttribute("parentSort",parentSort);
			NewsSort newsSort = new NewsSort();
			model.addAttribute("newsSort",newsSort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	@RequestMapping(value="/editReportSort",method=RequestMethod.GET)
	public String editReportSort(@RequestParam int sortId,Model model){
		editChildSort(sortId,model);
		return "/admin/news/addReportSort";
	}
	
	@RequestMapping(value="/editNewsSort",method=RequestMethod.GET)
	public String editNewsSort(@RequestParam int sortId,Model model){
		editChildSort(sortId,model);
		return "/admin/news/addNewsSort";
	}
	
	/**
	 * 跳转到编辑分类页面
	 * @param sortId
	 * @param model
	 * @return
	 */
	public void editChildSort(int sortId,Model model){
		
		try {
			NewsSort newsSort = newsSortService.getSortById(sortId);
			addChildSort(newsSort.getParentId(),model);
			
			model.addAttribute("newsSort",newsSort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	@RequestMapping(value="/delNewsSort",method=RequestMethod.GET)
	public String delNewsSort(@RequestParam int sortId,Model model){
		delSort(sortId,model);
		return "/admin/news/newsSort";
	}
	
	@RequestMapping(value="/delReportSort",method=RequestMethod.GET)
	public String delReportSort(@RequestParam int sortId,Model model){
		delSort(sortId,model);
		return "/admin/news/reportSort";
	}
    
	/**
	 * 删除分类
	 * @param sortId
	 * @return
	 */
	public void delSort(int sortId,Model model){
		try {
			String msg = newsSortService.delNewsSort(sortId);
			if(null != msg){
				model.addAttribute("message", msg);
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	@RequestMapping(value="/report",method=RequestMethod.POST)
    public String addOrUpdateReport(@ModelAttribute("form") NewsSort sort,Model model){
		try {
			sort.setModuleType(Constant.NEWS_TYPE_REPORT);
			newsSortService.addOrUpdateNewsSort(sort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "/admin/news/reportSort";
	}
	
	@RequestMapping(value="/news",method=RequestMethod.POST)
    public String addOrUpdateNews(@ModelAttribute("form") NewsSort sort,Model model){
		try {
			sort.setModuleType(Constant.NEWS_TYPE_NEWS);
			newsSortService.addOrUpdateNewsSort(sort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "/admin/news/newsSort";
	}
	
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/report/sorts",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<NewsSort> getAllReportSorts(){
		
		try {
			return newsSortService.getAllSorts(Constant.NEWS_TYPE_REPORT,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/news/sorts",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<NewsSort> getAllNewsSorts(){
		
		try {
			return newsSortService.getAllSorts(Constant.NEWS_TYPE_NEWS,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
}
